<?php

/**
 * @filename swoole_process3.php   进程队列通信
 * @encoding UTF-8 
 * @author  fwz 
 * @datetime 2018-6-12  17:27:10
  */


$worker = [];  //进程仓库
$worker_num = 2; //最大进程数

//批量创建进程
for($i=0;$i<$worker_num;$i++){
    $process = new swoole_process("doProcess",false,false);//创建子进程完成
    $process->useQueue(); //开启队列，类似于全局函数
    $pid = $process->start();
    $worker[$pid] = $process;
}


//进程执行函数
function doProcess(swoole_process $process){
    $recv = $process->pop();
    echo "pop === $recv \n ";
    
    sleep(5);
    $process->exit(0);
}

//主进程，向子进程添加数据
foreach($worker as $pid=>$process){
    $process->push(" push == $pid \n");
}

//等待子进程结束。回收资源
for($i=0;$i<$worker_num;$i++){
    $ret = swoole_process::wait();
    var_dump($ret);
}


